<?php
/**
*  +----------------------------------------------------------------------------------------------+
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------------------------------+
*   | Creater Time : 2013-10-15 	
*  +----------------------------------------------------------------------------------------------+
*    Link :		http://www.phpyrb.com
*					http://www.cloudsskill.com 
*					http://www.goshopx.com  
*					http://www.shopingj.com 	     
*  +----------------------------------------------------------------------------------------------+
**/

	class CheckproAction extends BaseAction{
		function _initialize(){
			parent::_initialize();
			$this->dao=M("check_product");
		}

		function index() {			
			$sql = 'SELECT a1.id,a3.name,a1.amount,a1.starttime,a1.endtime,a1.status,a1.point,a1.status,a1.period,a1.buy FROM check_product a1,product a2,product_description a3 where a1.product_id=a2.id and a2.id=a3.product_id   ';
			if($_REQUEST['name']){
				$sql .= "and a3.name like '%".$_REQUEST['name']."%'";
			}
			$attr = M ()->query ( $sql );
			
			$count = count($attr);//dump($count);die();
			$this->assign("totalCount",$count);
			$this->assign("name",$_REQUEST['name']);
			if($count>0){
				$page=new Page($count,$this->listrows);
				$sql .= " limit $page->firstRow,$page->listRows ";
				//echo $sql;die();
				$list = M()->query($sql);
				$this->assign("lista",$list);
			}

			$this->display();
		}

		function edit(){
			$id = $_REQUEST['id'];
			$attr=self::chepro($id);
			$this->assign("lista",$attr);
			$this->assign("module_name","Checkpro");
			$this->display();
		}
		function add(){
			$this->assign("module_name","Checkpro");
			$this->display();
		}
		/**
		 * 初始化模块信息
		 */
		function _setmod($model_name, $model_id) {
			$this->model_name = $model_name;
			$this->model_id = $model_id;
		}
		

		function chepro($id){
			if($id){
				$sql = 'SELECT a1.id,a3.name,a1.amount,a1.starttime,a1.endtime,a1.status,a1.point,a1.status,a1.period,a1.buy FROM check_product a1,product a2,product_description a3 where a1.product_id=a2.id and a2.id=a3.product_id and a1.id='.$id;
				$sql1 = 'SELECT count( a1.id ) AS num FROM check_product a1,product a2,product_description a3 where a1.product_id=a2.id and a2.id=a3.product_id and a1.id='.$id;
			}else{
				$sql = 'SELECT a1.id,a3.name,a1.amount,a1.starttime,a1.endtime,a1.status,a1.point,a1.status,a1.period,a1.buy FROM check_product a1,product a2,product_description a3 where a1.product_id=a2.id and a2.id=a3.product_id  and a1.status=1';
				$sql1 = 'SELECT count( a1.id ) AS num FROM check_product a1,product a2,product_description a3 where a1.product_id=a2.id and a2.id=a3.product_id  ';
			}
			$count = M ()->query ( $sql1 );
			$count = $count[0]['num'];
			$this->assign("totalCount",$count);
			if($count>0){
				$page=new Page($count,$this->listrows);
				$sql .= " limit $page->firstRow,$page->listRows ";
				//echo $sql;die();
				$list = M()->query($sql);
				
			}
			return $list;
		}
		function update(){
			//parent::update(M('check_product'),U("Checkpro/index"));
			$sql = "UPDATE `check_product` SET `amount`=".$_REQUEST['amount'].",`starttime`=".strtotime($_REQUEST['starttime']).",`endtime`=".strtotime($_REQUEST["endtime"]).",`status`=".$_REQUEST['status'].",`point`=".$_REQUEST['point'].",`period`='".$_REQUEST['period']."',`buy`=".$_REQUEST['buy']." WHERE id=".$_REQUEST['id'];
			$blag = M()->query($sql);
			if(is_array($blag)){
				parent::_ajm(200,'数据修改成功',0,$url?$url:U("Checkpro/index"));
			}else{
				parent::_ajm(300,'数据修改失败，请重试',0);
			}
			//parent::_ajm(300,$blag,0);
		}
		
		function insert(){
			$sql = "INSERT INTO `check_product`( `product_id`, `amount`, `starttime`, `endtime`, `status`, `point`, `period`, `buy`) VALUES (".$_REQUEST['product_id']." , ".$_REQUEST['amount']." , ' ".strtotime($_REQUEST['starttime'])." ' , ' ".strtotime($_REQUEST["endtime"])." ' , ".$_REQUEST['status']." , ".$_REQUEST['point']." , '".$_REQUEST['period']."',".$_REQUEST['buy'].")";
			M()->query($sql);
			$this->assign("module_name","Checkpro");
			$this->_ajm(200,'数据添加成功,',0,U("Checkpro/index"));
		}
		
		function product($num){
			if($num){
				$sql = 'SELECT a1.id,a2.name FROM product a1,product_description a2 where a1.id=a2.product_id GROUP BY a2.name LIMIT '.$num;
				$attr = M()->query($sql);
				return $attr;
			}
			
		}
		
		function product_num(){
				$sql = 'SELECT count( a1.id ) AS num FROM product a1, product_description a2 WHERE a1.id = a2.product_id ';
				$attr = M()->query($sql);
				return $attr;
		}
		function tree(){
			$sql = "SELECT a1.id,a2.name FROM product a1,product_description a2,product_to_category_tag a3 where a1.status = 1 and a1.id=a2.product_id  and a3.product_id = a1.id and a3.category_tag_id =3";
			$sql1 = "SELECT count(a1.id) AS num FROM product a1, product_description a2,product_to_category_tag a3 where a1.status = 1 and a1.id=a2.product_id  and a3.product_id = a1.id and a3.category_tag_id =3";
			if($_REQUEST['name']){
				$sql .= "and a2.name like '%".$_REQUEST['name']."%'";
				$sql1 .= "and a2.name like '%".$_REQUEST['name']."%'";
			}
			if($_REQUEST['id']){
				$sql .= "and a1.id = ".$_REQUEST['id'];
				$sql1 .= "and a1.id = ".$_REQUEST['id'];
			}
			if($_REQUEST['description']){
				$sql .= "and a2.description like '%".$_REQUEST['description']." %'";
				$sql1 .= "and a2.description like '%".$_REQUEST['description']." %'";
			}
			$count = M()->query($sql1);
			$count = $count[0]['num'];
			$this->assign("name",$_REQUEST['name']);
			$this->assign("id",$_REQUEST['id']);
			$this->assign("description",$_REQUEST['description']);
			$this->assign("totalCount",$count);
			if($count>0){
				$page=new Page($count,$this->listrows);
				$sql .= " limit $page->firstRow,$page->listRows ";
				//echo $sql;die();
				$list = M()->query($sql);
				$this->assign("tree",$list);
			}
			$this->display();
		}
		/*
			$num = (int)$nums[0]['num']/10;
			if (is_float($num) ) {
				$num = (int)$num;
				$num +=1;
			}
		*/
		
		function delall(){
			$dao = M('check_product');
			parent::delall($dao);
		}
		
	} 
/**
 * 树形菜单生成函数查找带找回的
 * @param array $arr 需要树形化的数组
 * @return string $str
 */
function baktree($arr){
	$str='';
	foreach ($arr as $key => $v) {
		$str.="<li><a href=\"javascript:\" onclick=\"\$.bringBack({id:'{$v['id']}', name:'{$v['name']}'})\">".$v['name'].'</a></li> ';
	}
	return $str;
}
	




